package com.wentao.mall.entity

import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Component
import java.util.*
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

@Component
class Utils {

    fun decrypt(encryptedData: String, sessionKey: String, iv: String): String {
//        val data = "15ZC8qjkYgfwR7/axypDWZ9kcKJSKOQ2yflQ6G7/AOllHLpR3U7thA+PR+aFoZ9+niQdtB7U6xVS3vQmU2vSHV9k4W8LsNAgJo7yCtfR91pZk+NI/A/EckX4buiJYJ/dgworJ4fT+dsS0FFyvitrbe8aXNito0nFIRSSoJSuh29rJHHLKt7H8p9u7/agPu9JCDC3zr8jkIBeFD0OWKZjyxqLfcC9i6SD+LYNodMzliD6u7XgD+iwaVn6FDnHFD14AJHVTodlcdeM+5zvlMPtZfJ3Kw0PcTivayM4lT62AByvW7PcLUyMrB5tZIG6SYHq6IIJQ/aI4YT7CkumjgA9izzLx5Pr+/TDdHGdc0xfw1OqI/N3G4yx+d0phLlp+AnZnC0Cfd7j64xhwDdE//CQkCK0lgWW3QPTRrTYaI+WYaDk6BwKlnwzgdxE7lvLK8kBrRqPPpjP6LnFpeVwzw6JqSOCtq/05F8Pd717iQyLLx2K1j6ScavcPCTlq+Ut1G65/hsE2E/EEo5DdFDML4IukQ=="
//        val iv = "trNisehlQM/J4JmVgTW2tg=="
//        val signature  = "6b0a8d0beb3ce9260e42fb2f39759b5a1aba182b"

        val decoder = Base64.getDecoder()

        val cipher = Cipher.getInstance("AES/CBC/NoPadding")
        val secretKeySpec = SecretKeySpec(decoder.decode(sessionKey), "AES")
        val ivParameterSpec = IvParameterSpec(decoder.decode(iv))

        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec)
        val final = cipher.doFinal(decoder.decode(encryptedData))
        return String(final)
    }

}

data class PageQuery(
    val current: Int = 1,
    val pageSize: Int = 10
) {
    fun toPageRequest(): PageRequest {
        return PageRequest.of(current - 1, pageSize)
    }

    fun toPageRequest(properties: String): PageRequest {
        return PageRequest.of(current - 1, pageSize, Sort.Direction.DESC, properties)
    }
}



data class ProTableResult(
    val success: Boolean,
    val data: List<Any>,
    val total: Long
)

inline fun <reified T : Any> Page<T>.simple(): ProTableResult {
    val content = this.content
    val totalElements = this.totalElements
    return ProTableResult(success = true, data = this.content, total = this.totalElements)
}